﻿Imports Microsoft.Xna.Framework

Public MustInherit Class BasicMenu
    Inherits GameScreen

    Private mTitle As String
    Private mSelectedIndex As Integer
    Private mResult As SimpleCallback
    Private mOnComplete As MenuCallback
    Private mBackgroundScreen As GameScreen
    Private mItems As New List(Of MenuItem)

    Public Delegate Sub MenuCallback(ByVal screen As GameScreen)

#Region " Properties "

    Public Overrides ReadOnly Property ScreenName() As String
        Get
            ScreenName = Title
        End Get
    End Property

    Public Property Title() As String
        Get
            Title = mTitle
        End Get
        Set(ByVal value As String)
            mTitle = value
        End Set
    End Property

    Public Property SelectedIndex() As Integer
        Get
            SelectedIndex = mSelectedIndex
        End Get
        Set(ByVal value As Integer)
            mSelectedIndex = value
        End Set
    End Property

    Protected Friend Property Result() As SimpleCallback
        Get
            Result = mResult
        End Get
        Set(ByVal value As SimpleCallback)
            mResult = value
        End Set
    End Property

    Protected Friend Property OnComplete() As MenuCallback
        Get
            OnComplete = mOnComplete
        End Get
        Set(ByVal value As MenuCallback)
            mOnComplete = value
        End Set
    End Property

    Public ReadOnly Property Items() As List(Of MenuItem)
        Get
            Items = mItems
        End Get
    End Property

    Public Property BackgroundScreen() As GameScreen
        Get
            BackgroundScreen = mBackgroundScreen
        End Get
        Set(ByVal value As GameScreen)
            mBackgroundScreen = value
        End Set
    End Property

#End Region ' Properties

    Public Sub New(ByVal GameManager As GameManager)
        MyBase.New(GameManager)
    End Sub

    Protected Sub Add(ByVal Text As String, ByVal OnSelect As SimpleCallback, ByVal Position As Vector2)
        mItems.Add(New MenuItem(Me, Text, OnSelect, Position))
    End Sub

    Public Overrides Sub Draw(ByVal elapsedSeconds As Single)
        MyBase.Draw(elapsedSeconds)

        If mBackgroundScreen IsNot Nothing Then mBackgroundScreen.Draw(elapsedSeconds)
        DrawBackgroundOverlay(elapsedSeconds)

        For Each mi As MenuItem In mItems
            mi.Draw()
        Next
    End Sub

    Public Overrides Sub HandleInput(ByVal input As IGamePadInputManager)
        MyBase.HandleInput(input)

        If input.BackPressed AndAlso mOnComplete IsNot Nothing Then mOnComplete.Invoke(BackgroundScreen)

        Select Case input.MenuMovement.Y
            Case Is < 0.0F
                SelectedIndex -= 1
            Case Is > 0.0F
                SelectedIndex += 1
        End Select

        If SelectedIndex > Items.Count - 1 Then SelectedIndex = 0
        If SelectedIndex < 0 Then SelectedIndex = Items.Count - 1

        If Items.Count > 0 Then mItems(mSelectedIndex).HandleInput(input)

    End Sub

    Public Overridable Sub DrawBackgroundOverlay(ByVal elapsedSeconds As Single)
    End Sub
End Class